package com.winning.dps.ds.service.common.impl;

import com.winning.dps.ds.config.multiDatasource.DataSource;
import com.winning.dps.ds.dao.common.CommonApiDao;
import com.winning.dps.ds.service.common.CommonApiService;
import com.winning.dps.ds.service.common.CommonLogApiService;
import common.entity.*;
import common.exception.BaseException;
import common.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @program: plat
 * @description: CommonApiServiceImpl 实现类
 * @author: nxs
 * @create: 2018-07-05 17:25
 **/
@Service
public class CommonApiServiceImpl implements CommonApiService {

    @Autowired
    private CommonApiDao commonApiDao;

    @Autowired
    private CommonLogApiService commonLogApiService;

//    List<SysEsbZcjgEntity> listJgList =  new ArrayList<>();
//
//    Map<String,String> pcodeTemp = new HashMap<>();

    @Override
    public List<TreeEntity> findJgTree(Map<String,Object> queryParams) {
        List<TreeEntity> list =  commonApiDao.findJgTree(queryParams);
        return list;
    }

    @DataSource(DataSource.index)
    @Override
    public List<TreeEntity> findZkJgTree(Map<String,Object> queryParams) {
        List<TreeEntity> list =  commonApiDao.findZkJgTree(queryParams);
        return list;
    }

    @DataSource(DataSource.index)
    @Override
    public List<TreeEntity> findZcjgTree(Map<String,Object> queryParams) {
      List<TreeEntity> list =  commonApiDao.findZcjgTree(queryParams);
      return list;
    }

    @Override
    @Transactional
    public SysUserEntity queryByUserName(String username) {
        return commonApiDao.queryByUserName(username);
    }
    @Override
    public List<TreeEntity> findjgtreeByPid(Map<String,Object> queryParams) {
        List<TreeEntity> list =  commonApiDao.findjgtreeByPid(queryParams);
        return list;
    }

    @Override
    public SysZcjgEntity getJgInfo(String jgdm) {
        SysZcjgEntity entity =  commonApiDao.getJgInfo(jgdm);
        return entity;
    }

    @Override
    public List<SysSjyzymlEntity> sysSjyzymlInfo(String zydm) {
        Calendar date = Calendar.getInstance();
        String year = String.valueOf(date.get(Calendar.YEAR));
        List<SysSjyzymlEntity> list = commonApiDao.sysSjyzymlInfo(zydm, year);
        return list;
    }

    @Override
    public  SysSjyzymlEntity sysSjyzymlObject(String zydm,String zdm) {
        return commonApiDao.sysSjyzymlObject(zydm, zdm);
    }



    @Override
    public List<SysYyzcEntity> queryApplication() {
        List<SysYyzcEntity> list =  commonApiDao.queryApplication();
        return list;
    }



    @Override
    public List<SysCzjsQxEntity> getCzryqx(String jgbm, String zclsh,String yydm) {
        List<SysCzjsQxEntity>  list  =  commonApiDao.getCzryqx(jgbm, zclsh,yydm);
        return list;
    }



    List<SysZcjgEntity> jgCommon;
    @Override
    public List<SysZcjgEntity> getSubJg(String jgdm) {

        List<SysZcjgEntity> list = commonApiDao.findAllJg();

        List<SysZcjgEntity> resultList = new ArrayList<SysZcjgEntity>();

        this.jgCommon = list;
        for (SysZcjgEntity sze : list) {
            if(jgdm.equals(sze.getSjjgbm())){
                resultList.add(sze);
                resultList.addAll(jgChild(sze.getJgbm()));
            }
        }
        return resultList;
    }


    public List<SysZcjgEntity> jgChild(String jgbm){
        List<SysZcjgEntity> lists = new ArrayList<SysZcjgEntity>();
        for(SysZcjgEntity sz:jgCommon){
            if(jgbm.equals(sz.getSjjgbm())){
                lists.add(sz);
                lists.addAll(jgChild(sz.getJgbm()));
            }
        }
        return lists;
    }




    @Override
    public SysQjcsEntity queryQjcs(String csbm) {

        SysQjcsEntity qjcsEntity = commonApiDao.queryQjcs(csbm);
        return qjcsEntity;
    }

    @Override
    @DataSource(DataSource.index)
    public SysQjcsEntity queryIndexQjcs(String csbm) {

      SysQjcsEntity qjcsEntity = commonApiDao.queryQjcs(csbm);
      return qjcsEntity;
    }

    @Override
    public void saveFileToDb(String tableName, String colName, byte[] bytes, String id, String idValue){
        commonApiDao.saveFileToDb(tableName,colName,bytes,id,idValue);
    }

    @Override
    public SysQjcsEntity queryCsbm(String csbm){
        return commonApiDao.queryQjcs(csbm);
    }

    @Override
    public List<SysEsbZcjgEntity> list(String jgbm) {
        Map<String,String> map = new HashMap<>(1);
        map.put("jgbm",jgbm);
        return getAllJg(map);
    }

    @Override
    @DataSource(DataSource.index)
    public List<SysEsbZcjgEntity> getAllJg(Map map) {
        return commonApiDao.getAllJg(map);
    }

    @Override
    public String getYwcjSjbzbm() {
        //所属业务场景
        SysQjcsEntity sysQjcsEntity = queryCsbm("sjzk_ssywcj");
        if (sysQjcsEntity != null) {
            if (Constant.YWCJ_YY.equals(sysQjcsEntity.getCsz())){
                return Constant.SJBZBM_YY;
            }else if(Constant.YWCJ_NH.equals(sysQjcsEntity.getCsz())){
                return Constant.YWCJ_NH;
            }else {
                return Constant.SJBZBM_QY;
            }
        }
        return Constant.SJBZBM_QY;
    }

    @Override
    @DataSource(DataSource.index)
    public SysEsbZcjgEntity getEsbJgInfo(String jgbm) {
        return commonApiDao.getEsbJgInfo(jgbm);
    }


    /**
     * 判断当前机构 是 卫健委 还是医院 true 表示  卫健委 false 表示医院 医院有下级的情况、虚拟卫生院或者卫健委
     */
    @Override
    @DataSource(DataSource.store)
    public boolean judgeWjjYy(String jgbm) {
        SysQjcsEntity qjcsEntity = queryQjcs("sjzk_ptJgbm");
        if (null == qjcsEntity) {
            throw new BaseException("全局参数：sjzk_ptJgbm未配置");
        }
        String ptJgbm = qjcsEntity.getCsz();
        SysZcjgEntity jgInfo = getJgInfo(StringUtils.replace(jgbm, "_XN", ""));
        SysEsbZcjgEntity esbJgInfo = commonLogApiService.getEsbJgInfo(jgbm);
        //("07".equals(esbJgInfo.getJglb()) && "1".equals(esbJgInfo.getXjjgbz()) || StringUtils.contains(jgbm, "_XN")) && "1".equals(tzbs))
        return jgbm.equalsIgnoreCase(ptJgbm) || "03".equals(jgInfo.getJglb())
                || ("07".equals(esbJgInfo.getJglb()) && "1".equals(esbJgInfo.getXjjgbz()) || StringUtils.contains(jgbm, "_XN"));
    }

    @Override
    @DataSource(DataSource.index)
    public Integer queryZkjgCount() {
        return commonApiDao.queryZkjgCount();
    }

    @Override
    @DataSource(DataSource.index)
    public Integer queryZkcsCount() {
        return commonApiDao.queryZkcsCount();
    }

    @Override
    public List<TreeEntity> findSjbzTree() {
        return commonApiDao.findSjbzTree();
    }

    @Override
    public String findYljgdmByUser(String jgbm) {
        List<Map> maps = commonApiDao.findYljgdmByUser(jgbm);
        String yljgdms = "";
        for(int i=0;i<maps.size();i++){
            yljgdms += maps.get(i).get("jgbm")+",";
        }
        return yljgdms.substring(0,yljgdms.length()-1);
    }


}
